package net.oschina.jnexus

/**
 * Created on 2015-11-07.
 *
 * @author dolphineor
 */
class IPv4HeaderVerifiable {

  implicit def hex2Int(hexStr: String): Int = Integer.parseInt(hexStr, 16)

  implicit def int2Hex(number: Int): String = Integer.toHexString(number)

  def checksum(header: String): String = {
    val headerArr = header split " "
    val hexSum = headerArr map (a => hex2Int(a)) reduce ((sum, a) => if (sum > 0xffff) sum - 0x10000 + 1 + a else sum + a)

    headerArr(5) = "ffff" - hexSum
    headerArr.mkString("", " ", "")
  }
}
